﻿using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO; 
using MyLib;

namespace CapexProcess.Dialogs
{
    /// <summary>
    /// GenerateReports 的摘要说明
    /// </summary>
    public class GetDeptApprovalMatrix : IHttpHandler 
    {
        string strJson;
        string strBudgetType;
        string strApplicantAccount;

        string strDeptS = "";

        string strDeptMan = ""; 
        string strDeptManAmount = "0";
        string strDeptDirector = ""; 
        string strDeptDirectorAmout = "0";
        string strDeptVP="";
        string strDeptVPAmout = "0";
        string strDeptGM = "";
        string strDeptGMAmout = "0";

        string strIsManager = ""; //是否是项目负责人

        public void ProcessRequest(HttpContext context)
        {
            string strPageLoad = "1";
            context.Response.ContentType = "text/plain";
            strBudgetType = context.Request["BudgetType"];            //Capex单上用户选择的预算类型  
            strApplicantAccount = context.Request["ApplicantAccount"];   //Capex单上申请人信息 
            if (strBudgetType != "")
            {
                string strResult = RenturnDeptManager(strApplicantAccount.Substring(5), strBudgetType, strPageLoad);
                if (strResult != "")
                {
                    JudgeLevel(strResult, strBudgetType);
                }
            }
                       
           


            //如果存在返回值的情况
           // if (dt.Rows.Count > 0)
           // {             
                 
                strJson = "[";
                //strJson += "{'DeptManager':'" + CheckText(strDeptMan.Trim()) + "'}";
                
                strJson += "{'DeptManager':'" + CheckText(strDeptMan.Trim()) + "',"
                        + "'DepatManagerAmount':'" + CheckText(strDeptManAmount.Trim()) + "',"
                        + "'DeptDirector':'" + CheckText(strDeptDirector.Trim()) + "',"
                        + "'DeptDirectorAmount':'" + CheckText(strDeptDirectorAmout.Trim()) + "',"
                        + "'DeptVP':'" + CheckText(strDeptVP.Trim()) + "',"
                        + "'DeptVPAmount':'" + CheckText(strDeptVPAmout.Trim()) + "',"
                        + "'DeptGM':'" + CheckText(strDeptGM.Trim()) + "',"
                        + "'DeptGMAmount':'" + CheckText(strDeptGMAmout.Trim()) + "'},";
                strJson = strJson.Substring(0, strJson.LastIndexOf(','));
                strJson += "]";
           // }

            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            context.Response.Expires = 0;
            context.Response.CacheControl = "no-cache";
            context.Response.Write(strJson);
        }


        /// <summary>
        /// 获取审批人员信息
        /// </summary>
        /// <param name="strDept"></param>
        /// <returns></returns>   
        public string RenturnDeptManager(string strRequest,string BudgetType,string strPCount)
        {
            //获取当前申请人是否是部门负责人和汇报对象
            string strDeptManager = "";
            DataTable dt = null;
            string strSql = "select t1.CNNAME,t2.USERID,t2.EXT01,t1.LOGINNAME,t2.DIRECTREPORTID,t2.ISMANAGER from dbo.COM_USER as t1 "
            + "inner join dbo.COM_JOB as t2 on t1.USERCODE=t2.USERID  "
           + " where t1.LOGINNAME='" + strRequest.Trim() + "' and t2.DIRECTREPORTID is not null ";
            dt = DataAccess.Instance("BizDB").ExecuteDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                //strDeptManager = dt.Rows[0]["LOGINNAME"].ToString().Trim();
                strDeptManager = dt.Rows[0]["DIRECTREPORTID"].ToString().Trim();
                strIsManager = dt.Rows[0]["ISMANAGER"].ToString().Trim();
            }



            //逐级获取申请人的汇报对象            
            string strResult = "";
            string strs = " select t4.LOGINNAME,t3.JOBGRADE,t3.EXT03,t3.EXT04 from COM_JOB as t3 "
                       + "  inner join COM_USER as t4 on t3.USERID=t4.USERID "
                       + "    where JOBID=" + strDeptManager + "";
            if (strDeptManager != "" && strIsManager == "0")   //判断如果当前人员不为空并且不是部门负责人时执行该方法
            {              
                DataTable dtAll = DataAccess.Instance("BizDB").ExecuteDataTable(strs);
                if (dtAll.Rows.Count > 0)
                {
                    strResult = dtAll.Rows[0]["JOBGRADE"].ToString().Trim();
                    if (strResult == "S")   //主管
                    {
                        strDeptS = "vast/" + dtAll.Rows[0]["LOGINNAME"].ToString().Trim();//审批人
                    }
                    else if (strResult == "M")  //经理
                    {
                        strDeptMan = "vast/" + dtAll.Rows[0]["LOGINNAME"].ToString().Trim();//审批人
                        if (BudgetType == "B")
                        {
                            strDeptManAmount = dtAll.Rows[0]["EXT03"].ToString().Trim();   //预算内
                        }
                        else
                        {
                            strDeptManAmount = dtAll.Rows[0]["EXT04"].ToString().Trim();   //预算外
                        }
                    }
                    else if (strResult == "D")   //总监
                    {
                        strDeptDirector = "vast/" + dtAll.Rows[0]["LOGINNAME"].ToString().Trim();//审批人
                        if (BudgetType == "B")
                        {
                            strDeptDirectorAmout = dtAll.Rows[0]["EXT03"].ToString().Trim();   //预算内
                        }
                        else
                        {
                            strDeptDirectorAmout = dtAll.Rows[0]["EXT04"].ToString().Trim();   //预算外
                        }
                    }
                    else if (strResult == "V")   //副总
                    {
                        strDeptVP = "vast/" + dtAll.Rows[0]["LOGINNAME"].ToString().Trim();//审批人
                        if (BudgetType == "B")
                        {
                            strDeptVPAmout = dtAll.Rows[0]["EXT03"].ToString().Trim();   //预算内
                        }
                        else
                        {
                            strDeptVPAmout = dtAll.Rows[0]["EXT04"].ToString().Trim();   //预算外
                        }

                    }
                    else if (strResult == "G")  //总经理
                    {
                        strDeptGM = "vast/" + dtAll.Rows[0]["LOGINNAME"].ToString().Trim();//审批人
                        if (BudgetType == "B")
                        {
                            strDeptGMAmout = dtAll.Rows[0]["EXT03"].ToString().Trim();   //预算内
                        }
                        else
                        {
                            strDeptGMAmout = dtAll.Rows[0]["EXT04"].ToString().Trim();   //预算外
                        }
                    }

                }
            }
            if (strPCount == "1" && strIsManager == "1")  //判断如果当前申请人就是部门负责人，则上升一级，查找该申请人的直接领导。
            {
                DataTable dtDept = DataAccess.Instance("BizDB").ExecuteDataTable(strs);
                if (dtDept.Rows.Count > 0)
                {
                    strDeptMan = "vast/" + dtDept.Rows[0]["LOGINNAME"].ToString().Trim();//审批人
                    if (BudgetType == "B")
                    {
                        strDeptManAmount = dtDept.Rows[0]["EXT03"].ToString().Trim();   //预算内
                    }
                    else
                    {
                        strDeptManAmount = dtDept.Rows[0]["EXT04"].ToString().Trim();   //预算外
                    }
                }
            }
            return strResult;
        }

        public void JudgeLevel(string Level, string BudgetType)
        {
            var TS = "";
            var TM = "";
            var TD = "";
            var Temp = "";
            var TempV = "";
            if (Level == "S")
            {
                if (strIsManager == "0")
                {
                    TS = RenturnDeptManager(strDeptS.Substring(5), BudgetType,"");
                    if (TS == "M")
                    {
                        if (strIsManager == "0")
                        {
                            TM = RenturnDeptManager(strDeptMan.Substring(5), BudgetType, "");
                        }
                    }
                    if (TS == "D")
                    {
                        if (strIsManager == "0")
                        {
                            TD = RenturnDeptManager(strDeptDirector.Substring(5), BudgetType, "");
                        }
                    }
                    if (TS == "V")
                    {
                        if (strIsManager == "0")
                        {
                            TempV = RenturnDeptManager(strDeptVP.Substring(5), BudgetType, "");
                        }
                    }
                    if (TempV == "G")
                    {
                        Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType, "");
                    }
                }
            }
            else if (Level == "M")
            {               
                if (strIsManager == "0")
                {
                    Temp = RenturnDeptManager(strDeptMan.Substring(5), BudgetType, "");
                    if (Temp == "D")
                    {
                        Temp = RenturnDeptManager(strDeptDirector.Substring(5), BudgetType, "");
                    }
                    if (Temp == "V")
                    {
                        TempV = RenturnDeptManager(strDeptVP.Substring(5), BudgetType, "");
                    }
                    if (TempV == "G")
                    {
                        Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType, "");
                    }
                }
            }
            else if (Level == "D")
            {                
                if (strIsManager == "0")
                {
                    Temp = RenturnDeptManager(strDeptDirector.Substring(5), BudgetType, "");
                    if (Temp == "V")
                    {
                        TempV = RenturnDeptManager(strDeptVP.Substring(5), BudgetType, "");
                    }
                    if (TempV == "G")
                    {
                        Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType, "");
                    }
                }
            }
            else if (Level == "V")
            {                
                if (strIsManager == "0")
                {
                    TempV = RenturnDeptManager(strDeptVP.Substring(5), BudgetType, "");
                    if (TempV == "G")
                    {
                        Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType, "");
                    }
                }
            }
            else if (Level == "G")
            {
                Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType, "");
            }
        }

        /// <summary>
        /// 判断岗位等级
        /// </summary>
        /// <param name="PostGrades"></param>
        /// <returns></returns>
        //public void JudgeLevel(string Level, string BudgetType)
        //{
        //    var TS = "";
        //    var TM = "";
        //    var TD = "";
        //    var Temp = "";
        //    var TempV = "";
        //    if (Level == "S")
        //    {
        //        TS = RenturnDeptManager(strDeptS.Substring(5), BudgetType);
        //        if (TS == "M")
        //        {
        //            TM = RenturnDeptManager(strDeptDirector.Substring(5), BudgetType);
        //        }
        //        if (TS == "D")
        //        {
        //            TD = RenturnDeptManager(strDeptDirector.Substring(5), BudgetType);
        //        }
        //        if (TS == "V")
        //        {
        //            TempV = RenturnDeptManager(strDeptVP.Substring(5), BudgetType);
        //        }
        //        if (TempV == "G")
        //        {
        //            Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType);
        //        }
        //    }
        //    else if (Level == "M")
        //    {
        //        Temp = RenturnDeptManager(strDeptMan.Substring(5), BudgetType);
        //        if (strIsManager == "")
        //        {
        //            if (Temp == "D")
        //            {
        //                Temp = RenturnDeptManager(strDeptDirector.Substring(5), BudgetType);
        //            }
        //            if (Temp == "V")
        //            {
        //                TempV = RenturnDeptManager(strDeptVP.Substring(5), BudgetType);
        //            }
        //            if (TempV == "G")
        //            {
        //                Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType);
        //            }
        //        }
        //    }
        //    else if (Level == "D")
        //    {
        //        Temp = RenturnDeptManager(strDeptDirector.Substring(5), BudgetType);
        //        if (strIsManager == "")
        //        {
        //            if (Temp == "V")
        //            {
        //                TempV = RenturnDeptManager(strDeptVP.Substring(5), BudgetType);
        //            }
        //            if (TempV == "G")
        //            {
        //                Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType);
        //            }
        //        }
        //    }
        //    else if (Level == "V")
        //    {
        //        TempV = RenturnDeptManager(strDeptVP.Substring(5), BudgetType);
        //        if (strIsManager == "")
        //        {

        //            if (TempV == "G")
        //            {
        //                Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType);
        //            }
        //        }
        //    }
        //    else if (Level == "G")
        //    {
        //        Temp = RenturnDeptManager(strDeptGM.Substring(5), BudgetType);
        //    }
        //}

        public string CheckText(string Str)
        {
            string ReturnString = "";
            ReturnString = Str.Replace(",", "#dou#");
            ReturnString = Str.Replace("'", "#dan#");
            return ReturnString;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
         
    }
}